﻿Imports System.ComponentModel
Imports Aricie.DNN.UI.Attributes
Imports Aricie.ComponentModel
Imports DotNetNuke.UI.WebControls
Imports System.Xml.Serialization
Imports Aricie.DNN.Services.Filtering
Imports Aricie.DNN.Entities

Namespace Aricie.DNN.Services.Url.FriendlyUrl


    <Serializable()> _
    Public Class SubFriendlierUrlStrategy
        Inherits FriendlierUrlStrategy
        Implements Aricie.ComponentModel.IMergeable(Of FriendlierUrlStrategy, FriendlierUrlStrategy)

#Region "Private members"

        Private _SubMatchingRegexs As Dictionary(Of Regex, SubFriendlierUrlStrategy)

#End Region

#Region "Public properties"

        <ExtendedCategory("OverrideSettings")> _
        <AutoPostBack()> _
        <SortOrder(0)> _
        <TrialLimited(DNN.Security.Trial.TrialPropertyMode.Disable)> _
        Public Property OverrideGeneralSettings() As Boolean = True

        <ExtendedCategory("OverrideSettings")> _
        <AutoPostBack()> _
        <TrialLimited(DNN.Security.Trial.TrialPropertyMode.Disable)> _
        Public Property OverrideCachingSettings() As Boolean = False
        
        <ExtendedCategory("OverrideSettings")> _
        <AutoPostBack()> _
        <TrialLimited(DNN.Security.Trial.TrialPropertyMode.Disable)> _
        Public Property OverrideHtmlFilterSettings() As Boolean = False
        
            
        <ExtendedCategory("OverrideSettings")> _
        <AutoPostBack()> _
        <TrialLimited(DNN.Security.Trial.TrialPropertyMode.Disable)> _
        Public Property OverrideConditionalSettings() As Boolean = True

        <ExtendedCategory("OverrideSettings")> _
        <AutoPostBack()> _
        <TrialLimited(DNN.Security.Trial.TrialPropertyMode.Disable)> _
        Public Property OverrideTechnicalSettings() As Boolean = False

        '<AutoPostBack()> _
        <ExtendedCategory("OverrideSettings")> _
        <TrialLimited(DNN.Security.Trial.TrialPropertyMode.Disable)> _
        Public Property AdditionalMatchingRegex() As String = String.Empty
            
#End Region

#Region "Visibility helpers"
        <Browsable(False)> _
        Public Overrides ReadOnly Property DisplayCachingSettings As Boolean
            Get
                Return OverrideCachingSettings
            End Get
        End Property

        <Browsable(False)> _
        Public Overrides ReadOnly Property DisplayConditionalBehavior As Boolean
            Get
                Return OverrideConditionalSettings
            End Get
        End Property

        <Browsable(False)> _
        Public Overrides ReadOnly Property DisplayGeneralSettings As Boolean
            Get
                Return OverrideGeneralSettings
            End Get
        End Property

        <Browsable(False)> _
        Public Overrides ReadOnly Property DisplayTechnicalSettings As Boolean
            Get
                Return OverrideTechnicalSettings
            End Get
        End Property

         <Browsable(False)> _
        Public Overrides ReadOnly Property DisplayHtmlFilter As Boolean
            Get
                Return OverrideHtmlFilterSettings
            End Get
        End Property

         


#End Region

#Region "Public methods"

        Public Sub FillRegexDictionary(ByRef dico As Dictionary(Of String, SubFriendlierUrlStrategy))
            If Me.AdditionalMatchingRegex <> "" Then
                dico(Me.AdditionalMatchingRegex) = Me
            End If
            For Each conditionalStrategy As ConditionalEntity(Of SubFriendlierUrlStrategy) In Me.ConditionalBehaviours
                If conditionalStrategy.Enabled Then
                    conditionalStrategy.Value.FillRegexDictionary(dico)
                End If
            Next
        End Sub

        Public Function GetRegexDictionary() As Dictionary(Of Regex, SubFriendlierUrlStrategy)
            If Me._SubMatchingRegexs Is Nothing Then
                Dim tempStrDico As New Dictionary(Of String, SubFriendlierUrlStrategy)
                Me.FillRegexDictionary(tempStrDico)

                Me._SubMatchingRegexs = New Dictionary(Of Regex, SubFriendlierUrlStrategy)
                For Each strRegex As String In tempStrDico.Keys
                    Dim objRegex As New Regex(strRegex, RegexOptions.IgnoreCase Or RegexOptions.CultureInvariant Or RegexOptions.Compiled)
                    Me._SubMatchingRegexs(objRegex) = tempStrDico(strRegex)
                Next
            End If
            Return Me._SubMatchingRegexs
        End Function

        Public Function GetMerge(ByVal parentConfig As FriendlierUrlStrategy) As FriendlierUrlStrategy Implements IMergeable(Of FriendlierUrlStrategy, FriendlierUrlStrategy).GetMerge
            Dim newKey As String = Me.cacheKey & parentConfig.cacheKey

            Dim toReturn As FriendlierUrlStrategy = Aricie.Services.CacheHelper.GetGlobal(Of FriendlierUrlStrategy)(newKey)

            If toReturn Is Nothing Then
                toReturn = Aricie.Services.ReflectionHelper.CloneObject(Of FriendlierUrlStrategy)(Me)
                If Not Me.OverrideGeneralSettings Then
                    toReturn.Enabled = parentConfig.Enabled
                    toReturn.FriendlyRule = parentConfig.FriendlyRule
                    toReturn.Enable301 = parentConfig.Enable301
                End If
                If Not Me.OverrideCachingSettings Then
                    toReturn.AbsoluteCacheDuration = parentConfig.AbsoluteCacheDuration
                    toReturn.EnableResponseCaching = parentConfig.EnableResponseCaching
                    toReturn.CachingStrategy = parentConfig.CachingStrategy
                End If
                If Not Me.OverrideHtmlFilterSettings Then
                    toReturn.HtmlFilter = parentConfig.HtmlFilter
                End If
                If Not Me.OverrideConditionalSettings Then
                    toReturn.ConditionalBehaviours = parentConfig.ConditionalBehaviours
                End If
                If Not Me.OverrideTechnicalSettings Then
                    toReturn.DiscardPortalAlias = parentConfig.DiscardPortalAlias
                    toReturn.ForcedAlias = parentConfig.ForcedAlias
                End If
                Aricie.Services.CacheHelper.SetCacheDependant(Of FriendlierUrlStrategy)(toReturn, FriendlierUrlContext.CACHING_DEPENDENCY, Constants.Cache.GlobalExpiration, newKey)
            End If
            Return toReturn
        End Function

#End Region



       
    End Class
End Namespace

